package com.ygp.dtp.common.config.threadpool;

import com.ygp.dtp.common.constant.DynamicTpConstant;
import com.ygp.dtp.common.dto.NotifyItem;
import com.ygp.dtp.common.enums.QueueTypeEnum;
import com.ygp.dtp.common.enums.RejectedTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @description: 线程池属性配置类
 * @motto: 代码源于生活，高于生活艺术
 * @author: zhouhengzhe
 * @date: 2022/8/4 16:19
 * @since 1.0
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ThreadPoolProperties {

    /**
     * 线程池名字
     */
    private String threadPoolName = "DynamicTp";

    /**
     * 线程池核心线程数
     * Core pool size is the minimum number of workers to keep alive (and not allow to time out etc)
     * unless allowCoreThreadTimeOut is set, in which case the minimum is zero.
     */
    private int corePoolSize = 1;

    /**
     * Maximum pool size. Note that the actual maximum is internally bounded by CAPACITY.
     * 线程池最大线程数
     */
    private int maximumPoolSize = DynamicTpConstant.AVAILABLE_PROCESSORS;

    /**
     * 阻塞队列大小（容量）
     */
    private int queueCapacity = 1024;

    /**
     * Handler called when saturated or shutdown in execute.
     * 阻塞队列类型
     */
    private String queueType = QueueTypeEnum.VARIABLE_LINKED_BLOCKING_QUEUE.getName();

    /**
     * 如果是公平，则用同步队列
     */
    private boolean fair = false;

    /**
     * 拒绝策略
     * Timeout in nanoseconds for idle threads waiting for work. Threads use this timeout
     * when there are more than corePoolSize present or if allowCoreThreadTimeOut. Otherwise they wait forever for new work.
     */
    private String rejectedHandlerType = RejectedTypeEnum.CALLER_RUNS_POLICY.getCode();

    /**
     * 活跃时间
     */
    private long keepAliveTime = 30;

    /**
     * 活跃时间单位
     */
    private TimeUnit unit = TimeUnit.SECONDS;

    /**
     * 是否允许核心线程超时
     */
    private boolean allowCoreThreadTimeOut = false;

    /**
     * 线程名前缀
     */
    private String threadNamePrefix = "dynamic-tp";

    /**
     * 提醒
     */
    private List<NotifyItem> notifyItems;
}
